import sys
import time
sys.path.append("D:/travodds/travodds_py/install/lib")
sys.path.append("D:/travodds/build/travoddsgen/test/generate_py/output/install/lib")
from travodds import *
from ShapeType import *

def publisher():
    # Create DomainParticipantFactory
    factory = DomainParticipantFactoryProxy.get_instance()
    if not factory:
        print("Failed to get DomainParticipantFactory instance", file=sys.stderr)
        return
    
    dp_qos = DomainParticipantQosProxy()
    ret = factory.get_default_participant_qos(dp_qos)
    if ret != RETCODE_OK:
        print("Failed to get default DomainParticipantQos", file=sys.stderr)
        return

    # Create DomainParticipant
    participant = factory.create_participant(
        0, 
        # DomainParticipantFactoryProxy.PARTICIPANT_QOS_DEFAULT(), 
        dp_qos,
        None, 
        StatusMaskProxy.NONE
    )
    if not participant:
        print("Failed to create DomainParticipant", file=sys.stderr)
        return

    # Create Publisher
    publisher = participant.create_publisher(
        DomainParticipantProxy.publisher_qos_default(), 
        None, 
        StatusMaskProxy.NONE
    )

    if not publisher:
        print("Failed to create Publisher", file=sys.stderr)
        return
    
    # Register Type
    type_support = ShapeTypeTypeSupport.get_instance()
    if not type_support:
        print("Failed to get ShapeTypeTypeSupport instance", file=sys.stderr)
        return

    ret = participant.registe_type(
        'ShapeType',
        type_support
    )
    if ret != RETCODE_OK:
        print("Failed to register ShapeType type", file=sys.stderr)
        return

    # Create Topic
    topic = participant.create_topic(
        'ExampleTopic', 
        'ShapeType',
        DomainParticipantProxy.topic_qos_default(), 
        None, 
        StatusMaskProxy.NONE
    )
    if not topic:
        print("Failed to create Topic", file=sys.stderr)
        return
    
    # Create DataWriter
    data_writer = publisher.create_datawriter(
        topic, 
        PublisherProxy.datawriter_qos_default(), 
        None, 
        StatusMaskProxy.NONE
    )
    if not data_writer:
        print("Failed to create DataWriter", file=sys.stderr)
        return
    
    # Create typed DataWriter
    typed_data_writer = ShapeTypeDataWriter(data_writer)
    if not typed_data_writer:
        print("Failed to create typed DataWriter", file=sys.stderr)
        return
    
    # write data
    shape = ShapeType()
    shape.x = 1
    shape.y = 2
    shape.shapesize = 30
    shape.color = 'RED'

    for i in range(1000):
        shape.x += 1
        shape.y += 2
        
        # ret = typed_data_writer.write(shape, InstanceHandleProxy.handle_nil())
        # if ret != RETCODE_OK:
        #     print("Failed to write data", file=sys.stderr)
        #     return
        
        print(f"Written ShapeType: x={shape.x}, y={shape.y}, shapesize={shape.shapesize}, color={shape.color}")
        # Sleep for 1 second
        time.sleep(1)


if __name__ == "__main__":
    publisher()
